import orderApi from '@/config/server/api/order'
import tool from '@/common/tool'

function list(params) {
  return orderApi.commission.information.list.fetch({
    ...params
  }).then(res => {
    return res
  })
}

function invoicing(params) {
  return orderApi.commission.information.invoicing.fetch({
    ...params
  }).then(res => {
    return res
  })
}

function notInvoicePrice(commissionId) {
  return orderApi.commission.information.notInvoicePrice.fetch({
    commissionId
  }).then(res => {
    return res
  })
}

function getVoucher(commissionId) {
  return orderApi.commission.information.getVoucher.fetch({
    commissionId
  }).then(res => {
    return res
  })
}

function writeoffInfo(commissionId) {
  return orderApi.commission.information.writeoffInfo.fetch({
    commissionId
  }).then(res => {
    return res
  })
}

function cancelWriteOff(writeOffId) {
  return orderApi.commission.information.cancelWriteOff.fetch({
    writeOffId
  }).then(res => {
    return res
  })
}

function invoicingInfo(commissionId) {
  return orderApi.commission.information.invoicingInfo.fetch({
    commissionId
  }).then(res => {
    return res
  })
}

function cancelInvoicing(invoicingId) {
  return orderApi.commission.information.cancelInvoicing.fetch({
    invoicingId
  }).then(res => {
    return res
  })
}

function writeOffBackList(housesId) {
  return orderApi.commission.information.writeOffBackList.fetch({
    housesId
  }).then(res => {
    return res
  })
}

function writeOffRoomList({ commissionId, orderBy }) {
  return orderApi.commission.information.writeOffRoomList.fetch({
    commissionId,
    orderBy
  }).then(res => {
    return res
  })
}

function writeOff(params) {
  return orderApi.commission.information.writeOff.fetch({
    ...params
  }).then(res => {
    return res
  })
}

function exportWriteOffRoom(commissionId) {
  let info = {
    commissionId,
    fileName: '待核销房源导入.xls'
  }
  return orderApi.commission.information.exportWriteOffRoom.fetch(info)
}

function importWriteOffRoom(commissionId) {
  let info = { commissionId }
  let upload = orderApi.commission.information.importWriteOffRoom
  upload.setData(info)
  return upload
}

function updateCompany(info) {
  return orderApi.commission.information.updateCompany.fetch(info)
}

function exportData(info) {
  return orderApi.commission.information.export.fetch(info)
}

function initSelection(list, totalPrice) {
  let selection = [], total = 0
  for (let item of list) {
    let num = Number(total) + Number(item.actualAgentCost)
    if (tool.precision(num) <= totalPrice) {
      selection.push(item)
      total = tool.precision(num)
    }
  }
  return { selection, total }
}

export default {
  list,
  invoicing,
  notInvoicePrice,
  getVoucher,
  writeoffInfo,
  cancelWriteOff,
  invoicingInfo,
  cancelInvoicing,
  writeOffBackList,
  writeOffRoomList,
  writeOff,
  exportWriteOffRoom,
  importWriteOffRoom,
  updateCompany,
  exportData,
  
  initSelection
}